package com.heima.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.heima.service.AccountService;
import com.heima.service.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.sql.DataSource;

//@Component("accountService")
//@Service("accountService")
//@Repository("accountService")
@Controller("accountService")
@Scope("prototype")
public class AccountServiceImpl implements AccountService {
//    @Value("laofang")
//    private String name;
//    @Value("18")
//    private Integer age;
    //使用springel表达式： ${key} 根据指定的key从ioc容器中获取value值并注入
    @Value("${name}")
    private String name;
    @Value("${age}")
    private Integer age;

//    public void setName(String name) {
//        this.name = name;
//    }
//
//    public void setAge(Integer age) {
//        this.age = age;
//    }

//    @Autowired
//    @Qualifier("accountDao2")
//    private AccountDao dao;

//    @Autowired(required = false)
//    @Qualifier("accountDao3")
//    private AccountDao dao;
    @Resource(name = "accountDao2")
    private AccountDao dao;
    @Autowired
    private DruidDataSource dataSource;
    public void add() {
//        System.out.println("add run....");
        dao.add();
        System.out.println(dataSource.getUsername()+dataSource.getPassword());
    }
    @Override
    public String toString() {
        return "AccountServiceImpl{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @PostConstruct
    public void init(){
        System.out.println("init run...");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("destroy run....");
    }
}
